Completed
Pull Request — master (#2)
by Luís
02:11 queued 01:06
created

RestInterface.update   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
c 1
b 0
f 1
nc 2
nop 3
dl 0
loc 31
rs 8.8571
1
import App from "app";
2
const _ = App.libs._;
3
4
class RestInterface {
5
    /**
6
     * @param {String} serviceName   name of the service (used to emmit events)
7
     * @param {String} basepath
8
     */
9
    constructor(serviceName, basepath) {
10
        this.serviceName = serviceName;
11
        this.basepath = basepath;
12
13
        this.Request = App.ServicesContainer.get("AJAX");
14
        this.EM = App.EventManager;
15
16
        this.collection = [];
17
    }
18
19
    /**
20
     * Get all itens
21
     * @method index
22
     * @param  {Object} data      querystring
23
     * @param  {Function} onSuccess
24
     * @param  {Function} onError
25
     * @return {null}
26
     */
27
    index(data, onSuccess, onError) {
28
        this.Request.send("get", this.basepath, data, onSuccess, onError);
29
    }
30
31
    /**
32
     * Fetch single item
33
     * @param {String|Number} id
34
     * @param {Function}      onSuccess
35
     * @param {Function}      onError
36
     */
37
    show(id, onSuccess, onError) {
38
        this.Request.send("get", this.basepath + "/" + id, {}, onSuccess, onError);
39
    }
40
41
    /**
42
     * Create a new item
43
     * @param {Object}   data
44
     * @param {Function} onSuccess
45
     * @param {Function} onError
46
     */
47
    store(data, onSuccess, onError) {
48
        this.Request.send(
49
            "post",
50
            this.basepath,
51
            data,
52
            (res, req) => {
53
                this.EM.notify(this.serviceName + ".create", res, req);
54
55
                if (typeof onSuccess === "function") {
56
                    onSuccess(res, req);
57
                }
58
            },
59
            (err, res, req) => {
60
                this.EM.notify(this.serviceName + ".create.error", err, res, req);
61
62
                if (typeof onError === "function") {
63
                    onError(err, res, req);
64
                }
65
            }
66
        );
67
    }
68
69
    /**
70
     * @param {Object}   data
71
     * @param {Function} onSuccess
72
     * @param {Function} onError
73
     */
74
    update(data, onSuccess, onError) {
75
        let id = null;
76
77
        if (data.hasOwnProperty("id")) {
78
            id = data.id;
79
        } else {
80
            id = this._extractId(data);
81
        }
82
83
        let payload = data;
84
        payload._method = "PUT";
85
86
        this.Request.send(
87
            "post",
88
            this.basepath + "/" + id,
89
            payload,
90
            (res, req) => {
91
                this.EM.notify(this.serviceName + ".update", res, req);
92
93
                if (typeof onSuccess === "function") {
94
                    onSuccess(res, req);
95
                }
96
            },
97
            (err, res, req) => {
98
                this.EM.notify(this.serviceName + ".update.error", err, res, req);
99
100
                if (typeof onError === "function") {
101
                    onError(err, res, req);
102
                }
103
            }
104
        );
105
    }
106
107
    /**
108
     * @param {Object}   data
109
     * @param {Function} onSuccess
110
     * @param {Function} onError
111
     */
112
    destroy(data, onSuccess, onError) {
113
        let id = null;
114
        if (data.hasOwnProperty("id")) {
115
            id = data.id;
116
        } else {
117
            id = this._extractId(data);
118
        }
119
120
        let payload = data;
121
        payload._method = "DELETE";
122
123
        this.Request.send(
124
            "post",
125
            this.basepath + "/" + id,
126
            payload,
127
            (res, req) => {
128
                this.EM.notify(this.serviceName + ".destroy", res, req);
129
130
                if (typeof onSuccess === "function") {
131
                    onSuccess(res, req);
132
                }
133
            },
134
            (err, res, req) => {
135
                this.EM.notify(this.serviceName + ".destroy.error", err, res, req);
136
137
                if (typeof onError === "function") {
138
                    onError(err, res, req);
139
                }
140
            }
141
        );
142
    }
143
}
144
145
export default RestInterface;